import { Http } from '@/utils';
import ControlService from '@/service/control-service';
import ApplyService from '../apply-service';
import MainMode from './main-form-mode';


/**
 * Main 部件服务对象
 *
 * @export
 * @class MainService
 */
export default class MainService extends ControlService {

    /**
     * 申请服务对象
     *
     * @type {ApplyService}
     * @memberof MainService
     */
    public entityService: ApplyService = new ApplyService({ $store: this.getStore() });

    /**
     * Creates an instance of MainService.
     * 
     * @param {*} [opts={}]
     * @memberof MainService
     */
    constructor(opts: any = {}) {
        super(opts);
        this.mode = new MainMode();
    }

    /**
     * 处理返回数据
     *
     * @param {*} response
     * @returns
     * @memberof MainService
     */
    public handleResponse(response: any) {
        response.data = this.handleData(response.data);
    }

    /**
     * 添加数据
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    public add(action: string, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            const _entityService: any = this.entityService;
            if (_entityService[action] && _entityService[action] instanceof Function) {
                result = _entityService[action](data, isloading);
            } else {
                result = this.entityService.create(data, isloading);
            }
            result.then((response) => {
                this.handleResponse(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 删除数据
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    public delete(action: string, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            const _entityService: any = this.entityService;
            if (_entityService[action] && _entityService[action] instanceof Function) {
                result = _entityService[action](data, isloading);
            } else {
                result = this.entityService.remove(data, isloading);
            }
            result.then((response) => {
                this.handleResponse(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 修改数据
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    public update(action: string, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            const _entityService: any = this.entityService;
            if (_entityService[action] && _entityService[action] instanceof Function) {
                result = _entityService[action](data, isloading);
            } else {
                result = this.entityService.update(data, isloading);
            }
            result.then((response) => {
                this.handleResponse(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 查询数据
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    public get(action: string, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            const _entityService: any = this.entityService;
            if (_entityService[action] && _entityService[action] instanceof Function) {
                result = _entityService[action](data, isloading);
            } else {
                result = this.entityService.get(data, isloading);
            }
            result.then((response) => {
                this.handleResponse(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

    /**
     * 加载草稿
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MainService
     */
    public loadDraft(action: string, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: Promise<any>;
            const _entityService: any = this.entityService;
            if (_entityService[action] && _entityService[action] instanceof Function) {
                result = _entityService[action](data, isloading);
            } else {
                result = this.entityService.getdraft(data, isloading);
            }
            result.then((response) => {
                this.handleResponse(response);
                resolve(response);
            }).catch(response => {
                reject(response);
            });
        });
    }

}